package com.vehicle.mq.queue;

import com.vehicle.common.redis.manager.CacheManager;
import com.vehicle.common.util.Jsons;
import com.vehicle.mq.message.BaseMessage;
import com.vehicle.mq.message.MailMessage;
import com.vehicle.mq.message.SmsMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * Created by yantingbin on 2017/12/1.
 */
public class EventQueue {
    private static final Logger LOGGER = LoggerFactory.getLogger(EventQueue.class);
    private final CacheManager cacheManager;
    private final int processingErrorRetryCount = 2;
    private final String queueName = "product";
    private final String processingQueueName = "process";
    private final long maxBakSize = 20000000;
    private final int awaitInmillis = 1000;

    public static final List<Type> MESSAGE_BEANS = Arrays.asList(SmsMessage.class,MailMessage.class);

    private final BaseMessageQueue WAIT_QUEUE = WaitQueue.getInstance();

    private final BaseMessageQueue PROCESSING_QUEUE = ProcessingQueue.getInstance();

    public EventQueue(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

    public void publishMessage(BaseMessage baseMessage){
        WAIT_QUEUE.push(baseMessage,this);
    }

    public BaseMessage next() {
        BaseMessage bm = WAIT_QUEUE.pop(this);
        PROCESSING_QUEUE.push(bm, this);
        return bm;
    }

    public void remove(String json) {
        cacheManager.lRem(processingQueueName, json);
    }

    public CacheManager getCacheManager() {
        return this.cacheManager;
    }

    public int getAwaitInmillis() {
        return this.awaitInmillis;
    }
}
